//
//  WmcAppStateManager.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/24.
//

import RxRelay
import UIKit
//import CoreTelephony

// MARK: - [WmcAppStateManager]

class WmcAppStateManager: NSObject {
    // Internal

    /// 单例实例化
    static let shared: WmcAppStateManager = {
        let manager = WmcAppStateManager()
        return manager
    }()

    /// 电话监听
//    lazy var callCenter: CTCallCenter = {
//        let callCenter = CTCallCenter()
//        return callCenter
//    }()

    var observerQueue: DispatchQueue = .init(label: "wmc.queue.WmcAppStateManager")

    // MARK: - 监听

    // MARK: 应用状态

    /// 应用激活
    var becomeActive: PublishRelay<Void> = .init()
    /// 应用回到前台
    var appEnterForeground: PublishRelay<Void> = .init()
    /// 应用回到后台
    var appEnterBackground: PublishRelay<Void> = .init()

    // MARK: 电话状态

    /// 电话触发
    var phoneCallState: PublishRelay<Void> = .init()

    func config() {
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.didBecomeActiveNotification),
                                               name: UIApplication.didBecomeActiveNotification,
                                               object: nil)
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.willEnterForegroundNotification),
                                               name: UIApplication.willEnterForegroundNotification,
                                               object: nil)
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.didEnterBackgroundNotification),
                                               name: UIApplication.didEnterBackgroundNotification,
                                               object: nil)
//        // 电话状态
//        callCenter.callEventHandler = { [weak self] _ in
//            guard let weakSelf = self else { return }
//            weakSelf.phoneCallState.accept(())
//        }
        
    }

    // Private

    // 应用回到前台
    @objc
    private func didBecomeActiveNotification() {
        log("应用激活")
        becomeActive.accept(())
    }

    @objc
    private func willEnterForegroundNotification() {
        log("应用回到前台")
        appEnterForeground.accept(())
    }

    // 应用回到后台
    @objc
    private func didEnterBackgroundNotification() {
        log("应用回到后台")
        appEnterBackground.accept(())
    }

    /// 日志系统
    fileprivate func log(_ log: String) {
        WmcLogManager.DebugLog(type: .appStateManager, log: log)
    }
}

/**
 该api回过不了审核
 
// MARK: - CXCallObserverDelegate

extension WmcAppStateManager: CXCallObserverDelegate {
    // 电话状态
    enum CallState: String {
        /// 拨打
        case calls
        /// 拒绝接听
        case hangUp
        /// 链接
        case link
        /// 未接听
        case notAnswer
        /// 新来电
        case newCall
        /// 保留接听
        case keepAnswering
        /// 另一个挂断
        case otherHangUp
        /// 保持链接
        case keepLink
        /// 对方挂断
        case opponentHangUp
    }

    /// 电话事务回调
    func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
        let outgoing = call.isOutgoing
        let onHold = call.isOnHold
        let hasConnected = call.hasConnected
        let hasEnded = call.hasEnded
        /*
         拨打:  outgoing :1  onHold :0   hasConnected :0   hasEnded :0
         拒绝:  outgoing :1  onHold :0   hasConnected :0   hasEnded :1
         链接:  outgoing :1  onHold :0   hasConnected :1   hasEnded :0
         挂断:  outgoing :1  onHold :0   hasConnected :1   hasEnded :1
         对方未接听时挂断：  outgoing :1  onHold :0   hasConnected :0   hasEnded :1

         新来电话:    outgoing :0  onHold :0   hasConnected :0   hasEnded :0
         保留并接听:  outgoing :1  onHold :1   hasConnected :1   hasEnded :0
         另一个挂掉:  outgoing :0  onHold :0   hasConnected :1   hasEnded :0
         保持链接:    outgoing :1  onHold :0   hasConnected :1   hasEnded :1
         对方挂掉:    outgoing :0  onHold :0   hasConnected :1   hasEnded :1
         */
        var callState: CallState?
        // 拨打
        if outgoing == true, onHold == false, hasConnected == false, hasEnded == false {
            callState = .calls
        }
        // 拒绝
        if outgoing == true, onHold == false, hasConnected == false, hasEnded == true {
            callState = .hangUp
        }
        // 链接
        if outgoing == true, onHold == false, hasConnected == true, hasEnded == false {
            callState = .link
        }
        // 挂断
        if outgoing == true, onHold == false, hasConnected == true, hasEnded == true {
            callState = .hangUp
        }
        // 对方未接听时挂断
        if outgoing == true, onHold == false, hasConnected == false, hasEnded == true {
            callState = .notAnswer
        }
        // 新来电话
        if outgoing == false, onHold == false, hasConnected == false, hasEnded == false {
            callState = .newCall
        }
        // 保留并接听
        if outgoing == true, onHold == true, hasConnected == true, hasEnded == false {
            callState = .keepAnswering
        }
        // 另一个挂掉
        if outgoing == false, onHold == false, hasConnected == true, hasEnded == false {
            callState = .otherHangUp
        }
        // 保持链接
        if outgoing == true, onHold == false, hasConnected == true, hasEnded == true {
            callState = .keepLink
        }
        // 对方挂掉
        if outgoing == false, onHold == false, hasConnected == true, hasEnded == true {
            callState = .opponentHangUp
        }
        if let callState = callState {
            YPGCD.Main { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.log("应用被来电打断，状态：\(callState.rawValue)")
                weakSelf.phoneCallState.accept(callState)
            }
        }
    }
}
*/
